<script setup lang="ts">
import { ref } from 'vue'
import type { Image } from '@/types/consult'
import type { UploaderAfterRead } from 'vant/lib/uploader/types'
import { Toast } from 'vant'
import { uploadFileService } from '@/api/consult/index'
import type { ToastWrapperInstance } from 'vant/lib/toast/types'
import { OrderType } from '@/enums'
defineProps<{
  isDisabled: boolean
  orderStatus?: OrderType
}>()
const text = ref('')
const emit = defineEmits<{
  (e: 'send-text', text: string): void
  (e: 'send-image', img: Image): void
}>()

// 发送文字消息
const sendText = () => {
  emit('send-text', text.value)
  text.value = ''
}

// 图片聊天
const sendImage: UploaderAfterRead = async (data) => {
  let t: ToastWrapperInstance
  // 排除多图上传数组情况
  if (Array.isArray(data)) return
  // 排除不存在情况
  if (!data.file) return
  t = Toast.loading({
    message: '正在上传...',
    duration: 0, // 设置为0不会自动关闭
  })
  try {
    const res = await uploadFileService(data.file)
    emit('send-image', res)
  } catch (error) {
    console.log(error)
    Toast.fail({
      message: '上传失败',
      duration: 3000,
    })
  } finally {
    // 关闭提示
    t.clear()
  }
}
</script>

<template>
  <div class="room-action">
    <div class="tip" v-if="isDisabled && orderStatus !== OrderType.ConsultComplete">
      <cp-icon name="consult-alarm"></cp-icon>
      <span class="bounce-text">
        <span
          v-for="(char, index) in '等待医生接单中...'"
          :key="index"
          :style="{ animationDelay: index * 0.1 + 's' }"
        >
          {{ char }}
        </span>
      </span>
    </div>
    <!-- 1. 输入框: 发送文字消息-->
    <van-field
      type="text"
      class="input"
      :border="false"
      placeholder="问医生"
      autocomplete="off"
      v-model="text"
      :disabled="isDisabled"
      @keyup.enter="sendText"
    ></van-field>
    <!-- 2. 图片上传： 发送图片消息 -->
    <van-uploader :preview-image="false" :disabled="isDisabled" :after-read="sendImage">
      <cp-icon class="icon" name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .tip {
    position: absolute;
    top: 22px;
    right: 60px;
    z-index: 999;
    color: var(--cp-price);
    font-style: italic;
    .icon {
      vertical-align: middle;
    }
    .bounce-text {
      display: inline-block;
      span {
        display: inline-block;
        animation: bounce 1s infinite ease-in-out;
      }
      @keyframes bounce {
        0%,
        100% {
          transform: translateY(0);
        }
        25% {
          transform: translateY(-2px);
        }
        75% {
          transform: translateY(2px);
        }
      }
    }
  }

  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
